package com.example.contentclient;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class SendMMSActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView iv_appendix;
    private ActivityResultLauncher<Intent> m_resultLauncher;
    private EditText phone;
    private EditText title;
    private EditText message;
    private Uri pic_uri;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_mms);
        iv_appendix = findViewById(R.id.iv_appendix);
        iv_appendix.setOnClickListener(this);
        phone = findViewById(R.id.et_phone);
        title = findViewById(R.id.et_phone);
        message = findViewById(R.id.et_phone);
        findViewById(R.id.btn_send_mms).setOnClickListener(this);
        m_resultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if(result!=null&&result.getResultCode()==RESULT_OK)
                {
                    Intent intent = result.getData();
                    pic_uri = intent.getData();
                    iv_appendix.setImageURI(pic_uri);

                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.iv_appendix:
                //设置跳转目的是获取数据
                Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
                //设置获取数据的类型
                intent.setType("image/*");
                m_resultLauncher.launch(intent);
                break;
            case R.id.btn_send_mms:
                sendMms(phone.getText().toString(),title.getText().toString(),message.getText().toString());
                
                break;
        }
    }

    private void sendMms(String phone, String title, String message) {



        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //intent的接受者将被允许读取Intent携带的 URI数据
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.putExtra("address",phone);
        intent.putExtra("title",title);
        intent.putExtra("sms_body",message);
        //彩信的图片附件
        intent.putExtra(Intent.EXTRA_STREAM,pic_uri);
        //设置彩信的附件为图片类型
        intent.setType("image/*");
        startActivity(intent);
        Toast.makeText(this, "请在弹窗中选择短信或者信息应用", Toast.LENGTH_SHORT).show();
    }
}
